import axios from "axios";
import { Message } from "element-ui";
import router from "../router/router"
const request = axios.create({
    baseURL:'http://localhost:8000/api/routes',
    timeout:5000,
    headers:{
        'Authorization': sessionStorage.getItem('token')
    }
})
request.interceptors.request.use(config=>{
    if(sessionStorage.token){
        config.headers.Authorization = sessionStorage.token
    }
    return config
})
request.interceptors.response.use(res=>{
    return res
},(error)=>{
    //Message的作用是在页面上方出现一个提示框,是饿了么里的一个应用
    Message.error(error.response.data)
    const {status} = error.response;
    if(status == 401){
        Message.error('token失效,请重新登录!')
        sessionStorage.removeItem('token')
        router.push('loginPage')
    }
    return Promise.reject(error)
})

export const call = (url,method,params,data)=>request({
    url:url,
    method:method,
    params:params,
    data:data
})